<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\model\AccountDailyStatModel;
use app\model\ClientAccountModel;
use app\model\KsAdvertiserModel;
use app\model\KsMaterialDailyReportExtModel;
use app\model\KsMaterialDailyReportModel;
use app\model\mongo\MongoKsVideoDayReportModel;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

class MongoReportFixWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('mongo_report_fix');
        // 设置参数
        $this->setDescription('补充mongo报表staff,project');
        // 设置参数
        $this->setHelp('php think mongo_report_fix');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        $output->writeln('=======补充mongo报表开始' . date('Y-m-d H:i:s', time()) . '=======');
        $redisKey = 'mongo:report:relation';
        $redis = Cache::store('redis')->handler();
//                        $ids = ClientAccountModel::where('media_id', 2)->column('id');
//                        foreach ($ids as $id) {
//                            $redis->rPush($redisKey, $id);
//                        }
//                        exit;
        $st = time() + 5 * 60;
        while (time() < $st) {
            $accountId = $redis->lPop($redisKey);
            if ($accountId) {
                $accountId = intval($accountId);
                $accounts = AccountDailyStatModel::where('account_id', $accountId)
                    ->where('stat_date', '>=', 20230301)->select()->toArray();
                if ($accounts) {
                    foreach ($accounts as $val) {
                        $userId = strval(KsAdvertiserModel::where('advertiser_id', $val['advertiser_id'])->value('user_id'));
                        $statDate = intval($val['stat_date']);
                        $datas = (new KsMaterialDailyReportModel())->where('account_id', $accountId)->where('stat_date', $statDate)->select();
                        foreach ($datas as $aa) {
                            $content = (new KsMaterialDailyReportExtModel())->where('report_id', $aa->getKey())->value('content');
                            $saveData = [
                                'cost' => floatval($aa->getAttr('cost')),
                                'is_sync' => date('Ymd') == $statDate ? 0 : 1,
                                'is_archive' => date('Ymd') == $statDate ? 0 : 1,
                            ];
                            $saveData['company_id'] = intval($aa->getAttr('company_id'));
                            $saveData['client_id'] = intval($aa->getAttr('client_id'));
                            $saveData['account_id'] = intval($aa->getAttr('account_id'));
                            $saveData['adv_id'] = strval($aa->getAttr('advertiser_id'));
                            $saveData['stat_date'] = $statDate;
                            $saveData['signature'] = strval($aa->getAttr('signature'));
                            $saveData['video_id'] = strval($aa->getAttr('video_id'));
                            $saveData['staff_id'] = intval($val['staff_id']);
                            $saveData['project_id'] = intval($val['project_id']);
                            $saveData['user_id'] = $userId;
                            $saveData['content'] = $content ? json_decode($content, true) : [];
                            $model = (new MongoKsVideoDayReportModel());
                            $model->setAttrs($saveData);
                            $model->save();
                        }
                    }
                }
                $this->getOutputMessage($accountId . ' 成功');
            } else {
                break;
            }
        }
        $output->writeln('=======巨量引擎-补充mongo报表结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
